package com.zt.rongda.asset.controller;

import com.zt.servlet.*;
import com.zt.rongda.asset.model.AssetTransfer;
import com.zt.rongda.asset.service.AssetTransferService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
@RequestMapping(value = "/assetTransfer")
public class AssetTransferController {

    @Autowired
    private AssetTransferService assetTransferService;

    @RequestMapping(value = "/query")
    @ResponseBody
    @DeptPermPrepare(id = "assetId", deptId = "deptId")
    public JSONObject query(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(assetTransferService.getTable(reqBean));
    }

    @RequestMapping(value = "/add")
    @ResponseBody
    @DeptPermValidate(id = "assetId", deptId = "deptId")
    public JSONObject add(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        assetTransferService.add(getAssetTransfer(reqBean));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/update")
    @ResponseBody
    @DeptPermValidate(id = "assetId", deptId = "deptId")
    public JSONObject update(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        assetTransferService.update(getAssetTransfer(reqBean));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    @DeptPermValidate(id = "assetId", deptId = "deptId")
    public JSONObject delete(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        assetTransferService.delete(reqBean.getInfo("assetId"));
        return Response.responseSuccess();
    }

    /**
    * 从表单获取对象
    *
    * @param reqBean
    * @return
    */
    private AssetTransfer getAssetTransfer(ReqBean reqBean) {
        AssetTransfer assetTransfer = new AssetTransfer();
        assetTransfer.setListId(reqBean.getInfo("listId"));
        assetTransfer.setAssetId(reqBean.getInfo("assetId"));
        assetTransfer.setUserId(reqBean.getInfo("userId"));
        assetTransfer.setUserName(reqBean.getInfo("userName"));
        assetTransfer.setDeptId(reqBean.getInfo("deptId"));
        assetTransfer.setDeptName(reqBean.getInfo("deptName"));
        assetTransfer.setUseDate(reqBean.getInfoDate("useDate"));
        assetTransfer.setLocation(reqBean.getInfo("location"));
        assetTransfer.setStatus(reqBean.getInfo("status"));
        assetTransfer.setOperator(reqBean.getUserId());
        assetTransfer.setRemark(reqBean.getInfo("remark"));
        return assetTransfer;
    }

}